package com.lz.ccjdemo.act;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.lz.ccjdemo.R;

import java.util.ArrayList;
import java.util.List;

/**********************************************************************
 * @desc 列表
 * @author lz
 * @data 2024/2/29 or 16:38
 *====================================================================*/
public class ListViewActivity extends Activity implements AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener {

    private List<String> list;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_view);
        ListView listView = findViewById(R.id.list_view);
        list = new ArrayList<>();
        for (int i = 0; i <100 ; i++) {
            list.add("tag"+i);
        }
        ListAdapter adapter = new ListAdapter(this, list);
        listView.setAdapter(adapter);
//        listView.setOnItemClickListener(this);
//        listView.setOnItemLongClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Toast.makeText(this, "点击了 " + list.get(position), Toast.LENGTH_SHORT).show();
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        Toast.makeText(this, "长按了 " + list.get(position), Toast.LENGTH_SHORT).show();
        return false;
    }

    public class ViewHolder {
        TextView tv1;
        TextView tv2;
    }

    public class ListAdapter extends BaseAdapter {

        private Context context;
        private List<String> list;

        public List<String> getList() {
            return list;
        }

        /**
         * 添加单个数据 参数随着数据类型变化
         *
         * @param str
         */
        public void addData(String str) {
            if (this.list == null) {
                this.list = new ArrayList<>();
            }
            list.add(str);
            notifyDataSetChanged();
        }

        /**
         * 添加列表数据
         *
         * @param list
         */
        public void addDataAll(List<String> list) {
            if (this.list == null) {
                this.list = new ArrayList<>();
            }
            this.list.addAll(list);
            notifyDataSetChanged();
        }

        /**
         * 更新原有数据列表，替换
         *
         * @param list
         */
        public void updateData(List<String> list) {
            this.list = list;
            notifyDataSetChanged();
        }

        public ListAdapter(Context context, List<String> list) {
            this.context = context;
            this.list = list;
        }

        @Override
        public int getCount() {
            return list == null ? 0 : list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = LayoutInflater.from(context).inflate(R.layout.item_forms, parent, false);
                holder.tv1 = convertView.findViewById(R.id.tv_name_tag);
                holder.tv2 = convertView.findViewById(R.id.btn_add);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.tv1.setText(list.get(position));
            return convertView;
        }
    }
}
